<template>
  <div class="app-container">

    <div class="filter-container">
      <el-input
        v-model="listQuery.username"
        placeholder="登录账号"
        style="width: 200px;"
        class="filter-item"
        @keyup.enter.native="handleFilter" />
      <el-input
        v-model="listQuery.realname"
        placeholder="用户名称"
        style="width: 200px;"
        class="filter-item"
        @keyup.enter.native="handleFilter" />
      <!-- <el-select v-model="listQuery.type" style="width: 200px;" class="filter-item">
        <el-option
          v-for="item in userType"
          :key="item.value"
          :label="item.label"
          :value="item.value" />
      </el-select> -->
      <el-button
        v-waves
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        @click="handleFilter">
        {{ lang.systemManage.user.index.filters.searchBtn }}
      </el-button>
      <el-button
        v-waves
        class="filter-item"
        type="default"
        icon="el-icon-refresh"
        @click="tableReset()">
        {{ lang.systemManage.user.index.filters.refreshBtn }}
      </el-button>
      <el-button v-permission="'sys-user-add'" v-waves class="filter-item right-btns" type="success" @click="edit()"><svg-icon icon-class="add"/> 新增</el-button>
      <el-button v-waves class="filter-item right-btns" type="success" @click="importStore()"><svg-icon icon-class="add"/> 导入</el-button>
    </div>

    <el-table
      v-loading="listLoading"
      :data="listQuery.list"
      :stripe="true"
      :border="true"
      fit
      highlight-current-row
      style="width: 100%;"
      @selection-change="handleSelectionChange">
      <el-table-column type="index" align="center"/>
      <el-table-column type="selection" width="55"/>
      <el-table-column :label="'店铺名称'" align="left" min-width="80px" prop="name" />
      <el-table-column :label="'区域名称'" align="left" min-width="80px" prop="quyu.name" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="160px">
        <template slot-scope="scope">
          <el-button v-waves type="primary" plain size="mini" @click="edit(scope.row)">修改</el-button>
          <el-button v-waves type="danger" plain size="mini" @click="del(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="listQuery.total>0"
      :total="listQuery.total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.rows"
      @pagination="getList" />
    <add-edit-dialog ref="editDialog" :loading="isEditSubmitting" @ok="handleEditOk"/>
    <import-dialog ref="importDialogRef" @import-success="getList"/>
  </div>
</template>

<script>
import { getPages, mendianUpdate, mendianAdd, mendianDel } from '@/api/systemManage/mendian'
import waves from '@/directive/waves' // Waves directive
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
import DelBoxMixin from '@/mixins/delBox'
import addEditDialog from './add-edit'
import importDialog from './importDialog'
import { mapGetters } from 'vuex'

export default {
  name: 'StoreManage',
  components: { Pagination, addEditDialog, importDialog },
  directives: { waves },
  mixins: [DelBoxMixin],

  props: {
    options: {
      type: Object,
      default: function() { return {} }
    }
  },

  data() {
    return {
      roleDialogVisible: false,
      userRoleTemp: { ids: null },
      roles: [],
      roleSelect: null,
      listLoading: true,
      listQuery: {},
      selectionedList: [],
      isEditSubmitting: false,
      isSetOrganSubmitting: false
    }
  },
  computed: {
    ...mapGetters([
      'lang'
    ])
  },
  created() {
    this.tableReset()
  },
  methods: {
    tableReset() {
      this.listQuery = {
        page: 1,
        rows: 10,
        total: 0,
        list: null
      }
      this.getList()
    },
    importStore() {
      this.$refs.importDialogRef.init(this.$route.meta.id)
    },
    getList() {
      this.listLoading = true
      const params = Object.assign({}, this.listQuery)
      delete params.total
      delete params.list

      getPages(params).then(response => {
        this.listQuery.list = response.data.list
        this.listQuery.total = response.data.total
        this.listLoading = false
      }).catch(e => {
        this.listLoading = false
        this.$message.error(e.message)
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleSelectionChange(val) {
      this.selectionedList = val
    },
    edit(item) {
      if (item && item.id) {
        this.$refs.editDialog.show({
          id: item ? item.id : 0
        })
      } else {
        this.$refs.editDialog.show()
      }
    },

    handleEditOk(data, isEdit) {
      this.isEditSubmitting = true
      const invoke = isEdit ? mendianUpdate : mendianAdd
      invoke(data).then(resp => {
        this.getList()
        this.$message.success(resp.message)
        this.isEditSubmitting = false
        this.$refs.editDialog.close()
      }).catch(resp => {
        this.$message.error(resp.message)
        this.isEditSubmitting = false
      })
    },
    del(item) {
      this.$$delBox().then(() => {
        mendianDel(item.id).then(res => {
          this.$message.success(res.message)
          this.getList()
        }).catch(e => {
          this.$message.error(e.message)
        })
      }).catch(() => {})
    }
  }
}
</script>
